using System;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Collections;
using System.Net;
using Transferomatic.Views;
using System.Diagnostics;
using System.Drawing;
using System.Deployment.Application;
using System.ComponentModel;
using Transferomatic.Properties;
using Transferomatic.Enumerators;

namespace Transferomatic.ApplicationControllers
{
    public partial class Controller
    {
        #region Methods
        private void FileTransferView_Show(
            MainView theParentView)
        {
            // Instantiate an instance
            FileTransferView theView = new FileTransferView();

            theView.buttonOpenSource.Click += 
                new EventHandler(FileTransferView_buttonOpenSource_Click);
            theView.buttonRemoveSelected.Click +=
                new EventHandler(FileTransferView_buttonRemoveSelected_Click);
            theView.gridSourceFiles.MouseUp += 
                new MouseEventHandler(FileTransferView_gridSourceFiles_MouseUp);
            theView.menuitemRemoveFromSelection.Click += 
                new EventHandler(FileTransferView_menuitemRemoveFromSelection_Click);
            theView.buttonOpenDestination.Click += 
                new EventHandler(FileTransferView_buttonOpenDestination_Click);
            theView.buttonOK.Click += 
                new EventHandler(FileTransferView_buttonOK_Click);

            theView.openfiledialogSource.InitialDirectory =
                RegistryAgent.LastFileTransferSourceDirectory;
            theView.folderbrowserdialogDestination.SelectedPath =
                theView.textboxDestination.Text = 
                    RegistryAgent.LastFileTransferDestinationDirectory;

            // Show the form
            theView.ShowDialog(theParentView);
        }

        private void FileTransferView_Show(
            MainView theParentView,
            TransferAgent theAgent)
        {
            TransferAgent theTransferAgent =
                theAgent as TransferAgent;

            // Instantiate an instance
            FileTransferView theView = new FileTransferView();

            theView.buttonOpenSource.Click += 
                new EventHandler(FileTransferView_buttonOpenSource_Click);
            theView.buttonRemoveSelected.Click +=
                new EventHandler(FileTransferView_buttonRemoveSelected_Click);
            theView.gridSourceFiles.MouseUp += 
                new MouseEventHandler(FileTransferView_gridSourceFiles_MouseUp);
            theView.menuitemRemoveFromSelection.Click += 
                new EventHandler(FileTransferView_menuitemRemoveFromSelection_Click);
            theView.buttonOpenDestination.Click += 
                new EventHandler(FileTransferView_buttonOpenDestination_Click);
            theView.buttonOK.Click += 
                new EventHandler(FileTransferView_buttonOK_Click);

            theView.Tag = theTransferAgent;

            theView.openfiledialogSource.InitialDirectory =
                theTransferAgent.SourceFiles[0].DirectoryName;

            foreach (FileInfo thisFileInfo in theTransferAgent.SourceFiles)
            {
                theView.gridSourceFiles.Rows.Add(
                    thisFileInfo.FullName);
            }

            theView.folderbrowserdialogDestination.SelectedPath =
                theView.textboxDestination.Text =
                    theTransferAgent.Destination.FullName;

            theView.buttonOK.Enabled = true;

            // Show the form
            theView.ShowDialog(theParentView);
        }
        
        #region Event Handlers
        private void FileTransferView_buttonOpenSource_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theOpenSourceButton =
                sender as Button;
            FileTransferView theParentView =
                theOpenSourceButton.FindForm() as FileTransferView;

            switch (theParentView.openfiledialogSource.ShowDialog())
            {
                case DialogResult.Cancel:
                    Application.DoEvents();
                    return;
            }

            foreach (string thisFileName in
                theParentView.openfiledialogSource.FileNames)
            {
                FileInfo thisFileInfo = new FileInfo(thisFileName);

                theParentView.gridSourceFiles.Rows.Add(
                    thisFileInfo.FullName);

                RegistryAgent.LastFileTransferSourceDirectory =
                    thisFileInfo.Directory.FullName;
            }

            theParentView.gridSourceFiles.ClearSelection();

            theParentView.buttonOK.Enabled =
                !theParentView.gridSourceFiles.Rows.Count.Equals(0) &&
                !String.IsNullOrEmpty(theParentView.textboxDestination.Text);
        }

        private void FileTransferView_buttonRemoveSelected_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theRemoveSelectedButton =
                sender as Button;
            FileTransferView theParentView =
                theRemoveSelectedButton.FindForm() as FileTransferView;

            if (!theParentView.gridSourceFiles.SelectedRows.Count.Equals(0))
            {
                foreach (DataGridViewRow thisRow in theParentView.gridSourceFiles.SelectedRows)
                {
                    theParentView.gridSourceFiles.Rows.Remove(thisRow);
                }

                theParentView.gridSourceFiles.ClearSelection();
            }

            theParentView.buttonOK.Enabled =
                !theParentView.gridSourceFiles.Rows.Count.Equals(0) &&
                !String.IsNullOrEmpty(theParentView.textboxDestination.Text);
        }

        private void FileTransferView_gridSourceFiles_MouseUp(
            object sender, 
            MouseEventArgs e)
        {
            Application.DoEvents();

            DataGridView theSourceGrid =
                sender as DataGridView;
            FileTransferView theParentView =
                theSourceGrid.FindForm() as FileTransferView;

            theParentView.menuitemRemoveFromSelection.Enabled =
                !theSourceGrid.SelectedRows.Count.Equals(0);
        }

        private void FileTransferView_menuitemRemoveFromSelection_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            ToolStripMenuItem theRemoveFromSelectionMenuItem =
                sender as ToolStripMenuItem;
            ContextMenuStrip theContextMenuStrip =
                theRemoveFromSelectionMenuItem.Owner as ContextMenuStrip;
            FileTransferView theParentView =
                theContextMenuStrip.SourceControl.FindForm()
                    as FileTransferView;

            this.FileTransferView_buttonRemoveSelected_Click(
                theParentView.buttonRemoveSelected, e);
        }

        private void FileTransferView_buttonOpenDestination_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theOpenDestinationButton =
                sender as Button;
            FileTransferView theParentView =
                theOpenDestinationButton.FindForm() as FileTransferView;

            switch (theParentView.folderbrowserdialogDestination.ShowDialog())
            {
                case DialogResult.Cancel:
                    Application.DoEvents();
                    return;
            }

            DirectoryInfo theDestinationDirectory = new DirectoryInfo(
                theParentView.folderbrowserdialogDestination.SelectedPath);

            RegistryAgent.LastFileTransferDestinationDirectory =
                theDestinationDirectory.FullName;

            theParentView.textboxDestination.Text =
                theDestinationDirectory.FullName;

            theParentView.buttonOK.Enabled =
                !theParentView.gridSourceFiles.Rows.Count.Equals(0) &&
                !String.IsNullOrEmpty(theParentView.textboxDestination.Text);
        }

        private void FileTransferView_buttonOK_Click(
            object sender, 
            EventArgs e)
        {
            Button theOKButton =
                sender as Button;
            FileTransferView theParentView =
                theOKButton.FindForm() as FileTransferView;
            TransferAgent theAgent =
                theParentView.Tag as TransferAgent;

            Application.DoEvents();

            theParentView.Cursor = Cursors.WaitCursor;

            if (null != theAgent)
            {
                for (int i = this._MainView.gridTransfers.Rows.Count - 1; i > -1; i--)
                {
                    TransferAgent thisAgent =
                        this._MainView.gridTransfers.Rows[i].Tag
                            as TransferAgent;

                    if (thisAgent.ID.Equals(theAgent.ID))
                    {
                        this._MainView.gridTransfers.Rows.RemoveAt(i);
                    }
                }
            }

            TransferAgent theNewAgent = new TransferAgent(
                TransferAgentType.File,
                theParentView.Action,
                RegistryAgent.BufferSize,
                new FileInfo(theParentView.gridSourceFiles.Rows[0].Cells[0].Value.ToString()).Directory,
                new DirectoryInfo(theParentView.textboxDestination.Text));

            theNewAgent.Transferring +=
                new EventHandler(TransferAgent_Transferring);
            theNewAgent.Paused +=
                new EventHandler(TransferAgent_Paused);
            theNewAgent.Failed +=
                new EventHandler(TransferAgent_Failed);
            theNewAgent.Completed +=
                new EventHandler(TransferAgent_Completed);
            theNewAgent.Cancelled +=
                new EventHandler(TransferAgent_Cancelled);
            theNewAgent.StatusChanged +=
                new EventHandler(TransferAgent_StatusChanged);

            DirectoryInfo theDestinationDirectoryInfo =
                new DirectoryInfo(theParentView.textboxDestination.Text);

            ExtendedDialogResult theExtendedDialogResult =
                ExtendedDialogResult.NotSet;

            foreach (DataGridViewRow thisRow in theParentView.gridSourceFiles.Rows)
            {
                FileInfo thisSourceFileInfo = new FileInfo(
                    thisRow.Cells[0].Value.ToString());
                FileInfo thisDestinationFileInfo = new FileInfo(
                    theDestinationDirectoryInfo.FullName + @"\" + thisSourceFileInfo.Name);

                if (thisDestinationFileInfo.Exists)
                {
                    if (theExtendedDialogResult.Equals(ExtendedDialogResult.NotSet) ||
                        theExtendedDialogResult.Equals(ExtendedDialogResult.Ignore) ||
                        theExtendedDialogResult.Equals(ExtendedDialogResult.Overwrite))
                    {
                        theExtendedDialogResult =
                            this.ConfirmationDialogView_Show(
                                this._MainView,
                                String.Format(
                                    CultureInfo.CurrentCulture,
                                    "File '{0}' already exists with a size of {1}Mb\n\n" +
                                    "What do you want to do?",
                                    thisDestinationFileInfo.FullName,
                                    Math.Round((decimal)thisDestinationFileInfo.Length / 1048576, 2)));

                        Application.DoEvents();
                    }

                    switch (theExtendedDialogResult)
                    {
                        case ExtendedDialogResult.Overwrite:
                        case ExtendedDialogResult.OverwriteAll:
                            thisDestinationFileInfo.Delete();
                            continue;
                        case ExtendedDialogResult.Ignore:
                        case ExtendedDialogResult.IgnoreAll:
                            continue;
                    }
                }

                theNewAgent.SourceFiles.Add(thisSourceFileInfo);
            }

            if (theNewAgent.SourceFiles.Count.Equals(0))
            {
                MessageBox.Show(
                    theParentView,
                    "There no files to transfer",
                    theParentView.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information,
                    MessageBoxDefaultButton.Button1);

                Application.DoEvents();
            }
            else
            {
                int thisIndex =
                    this._MainView.gridTransfers.Rows.Add(
                        Resources.page_white_copy,
                        theNewAgent.Action,
                        theNewAgent.SourceDescription,
                        theNewAgent.Destination.FullName,
                        String.Empty,
                        "0%",
                        String.Empty);

                this._MainView.gridTransfers.Rows[thisIndex].Tag = theNewAgent;

                this._MainView.gridTransfers.ClearSelection();

                this.UpdateControlButtons();
            }

            theParentView.Cursor = Cursors.Default;

            theParentView.Close();
        }
        #endregion
        #endregion
    }
}
